package com.tangsm.spring.boot.springdoc.domain.base;

import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serializable;

/**
 * 通用返回对象
 *
 * @param <T> 业务模型
 * @author tangsm
 * @date 2021-07-27 10:29:57
 */
@Schema(name = "Result", description = "通用返回对象")
public class Result<T> implements Serializable {
    /**
     * 有参构造方法
     * @param code 状态码
     * @param message 提示信息
     * @param data 数据封装
     */
    public Result(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 状态码
     */
    @Schema(name = "code", description = "状态码")
    private long code;

    /**
     * 提示信息
     */
    @Schema(name = "message", description = "提示信息")
    private String message;

    /**
     * 数据封装
     */
    @Schema(name = "data", description = "数据封装")
    private T data;

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(200, "操作成功", data);
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> failed() {
        return new Result<T>(400, "操作失败", null);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> Result<T> failed(String message) {
        return new Result<T>(400, message, null);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
